package com.ruoyi.framework.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * 验证配置
 *
 * @author xueyu
 */
@Slf4j
@Configuration
public class VerificationConfig {


    /**
     * 创建默认的验证工厂Bean
     *
     * @param applicationContext 应用上下文，用于获取应用中的资源
     * @return 返回配置好的LocalValidatorFactoryBean实例，该实例已设置好消息插值器
     */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public static LocalValidatorFactoryBean validatorFactoryBean(ApplicationContext applicationContext) {
        log.info("开始配置JSR380验证器。");
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(applicationContext);
        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
        factoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
        return factoryBean;
    }


}
